import { createApp } from "vue";
import App from "./App.vue";
import router from "./router";
import ElementPlus, { ElMessage } from "element-plus";

import "element-plus/dist/index.css";
import store from "./store";
import Avue from "@smallwei/avue";
import "./mock/index";
import "@smallwei/avue/lib/index.css";

import VueParticles from "vue-particles";
import { createPinia } from "pinia";

const debounce = (fn, delay) => {
  let timer = null;
  return function () {
    let context = this;
    let args = arguments;
    clearTimeout(timer);
    timer = setTimeout(function () {
      fn.apply(context, args);
    }, delay);
  };
};

const _ResizeObserver = window.ResizeObserver;
window.ResizeObserver = class ResizeObserver extends _ResizeObserver {
  constructor(callback) {
    callback = debounce(callback, 16);
    super(callback);
  }
};

createApp(App)
  .use(store)
  .use(VueParticles)
  .use(ElementPlus)
  .use(router)
  .use(Avue)
  .use(createPinia())
  .directive("copy", {
    mounted(el, { value }) {
      el.addEventListener("click", function () {
        // 创建输入框接受一个输入值
        const text = document.createElement("textarea");
        text.value = value;
        document.body.appendChild(text);
        text.select();
        const result = document.execCommand("Copy");
        if (result) {
          ElMessage({
            message: "复制成功",
            type: "success",
          });
          document.body.removeChild(text);
        }
      });
    },
  })
  .mount("#app");
